﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharpObjects.Interfaces;

namespace SharpObjects.RuleEngine
{
    [Serializable]
    public class RuleEngineContext : IRuleEngineContext
    {
        public Dictionary<string, object> Data = new Dictionary<string, object>();

        #region IRuleEngineContext Members

        public void SetValue<T>(string path, T value)
        {
            this.Data[path] = value;
        }

        public void SetValues<T>(string path, List<T> values)
        {
            this.Data[path] = values;
        }

        public T GetValue<T>(string path)
        {
            if(this.Data.ContainsKey(path))
            {
                return (T)this.Data[path];
            }
            else
            {
                throw new ArgumentException(string.Format("The path '{0}' does not exist in the RuleEngineContext", path));
            }
        }


        public T GetValue<T>(string path, T defaultValue)
        {
            if (this.Data.ContainsKey(path))
            {
                return (T)this.Data[path];
            }
            else
            {
                return (T)defaultValue;
            }
        }

        public List<T> GetValues<T>(string path)
        {
            if (this.Data.ContainsKey(path))
            {
                if (this.Data[path] is IEnumerable)
                {
                    IEnumerable array = (IEnumerable)this.Data[path];
                    return (from object value in array select (T) value).ToList();
                }
                else
                {
                    throw new ArgumentException(string.Format("Value at the path '{0}' does not implement IEnumerable", path));
                }
            }
            else
            {
                throw new ArgumentException(string.Format("The path '{0}' does not exist in the RuleEngineContext", path));
            }
        }

        #endregion      
    }
}
